package com.lzb.play.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lzb.play.R;
import com.lzb.play.adapter.SelBirdAdapter;
import com.lzb.play.basic.dialog.BaseSmartDialog;
import com.lzb.play.basic.media.AudioPlayerImpl;
import com.lzb.play.bean.SelBirdBean;
import com.lzb.play.databinding.DialogSelBirdBinding;

import java.util.ArrayList;
import java.util.List;

public class SelBirdDialog extends BaseSmartDialog<DialogSelBirdBinding> implements View.OnClickListener {

    private SelBirdAdapter selBirdAdapter;
    private AudioPlayerImpl mAudioPlayer;
    private AudioPlayerImpl mCurAudioPlayer;

    public SelBirdDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_sel_bird;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvCancel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);

        mAudioPlayer = new AudioPlayerImpl();
        mCurAudioPlayer = new AudioPlayerImpl();

        List<SelBirdBean> listData = new ArrayList<>();
        listData.add(new SelBirdBean(R.drawable.maque3, "麻雀", R.raw.maotouying));
        listData.add(new SelBirdBean(R.drawable.bailu3, "白鹭", R.raw.bailu));
        listData.add(new SelBirdBean(R.drawable.laoying3, "老鹰", R.raw.laoying));
        listData.add(new SelBirdBean(R.drawable.zhuomuniao3, "啄木鸟", R.raw.zhuomuniao));
        listData.add(new SelBirdBean(R.drawable.gezhi3, "鸽子", R.raw.gezi));
        listData.add(new SelBirdBean(R.drawable.yingwu3, "鹦鹉", R.raw.yingwu));
        listData.add(new SelBirdBean(R.drawable.xique3, "喜鹊", R.raw.xique));
        listData.add(new SelBirdBean(R.drawable.kongque3, "孔雀", R.raw.kongque));
        listData.add(new SelBirdBean(R.drawable.maotouying3, "猫头鹰", R.raw.maotouying));
        listData.add(new SelBirdBean(R.drawable.bailingniao3, "百灵鸟", R.raw.bailingniao));
        listData.add(new SelBirdBean(R.drawable.huangli3, "黄鹂", R.raw.huangli));
        listData.add(new SelBirdBean(R.drawable.huangque3, "黄雀", R.raw.huangque));
        listData.add(new SelBirdBean(R.drawable.jiayan3, "家燕", R.raw.jiayan));
        listData.add(new SelBirdBean(R.drawable.fengniao3, "蜂鸟", R.raw.fengniao));
        listData.add(new SelBirdBean(R.drawable.liuying3, "柳莺", R.raw.liuying));
        listData.add(new SelBirdBean(R.drawable.cuiniao3, "翠鸟", R.raw.cuiniao));
        listData.add(new SelBirdBean(R.drawable.quejiu3, "鹊鸲", R.raw.quequ));
        listData.add(new SelBirdBean(R.drawable.shanbanjiu3, "山斑鸠", R.raw.shanbanjiu));
        listData.add(new SelBirdBean(R.drawable.huamei3, "画眉", R.raw.huamei));
        listData.add(new SelBirdBean(R.drawable.haiou3, "海鸥", R.raw.haiou));

        mDataBinding.rvBirdList.setLayoutManager(new GridLayoutManager(getContext(), 4));

        selBirdAdapter = new SelBirdAdapter(listData, mAudioPlayer);
        selBirdAdapter.mCurAudioPlayer = mCurAudioPlayer;

        mDataBinding.rvBirdList.setAdapter(selBirdAdapter);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvCancel) {
            dismiss();
        } else if (id == R.id.tvConfirm) {
            int position = selBirdAdapter.getPosition();
            if (position == -1) {
                ToastUtils.showShort(getContext().getString(R.string.please_sel_bird));
                return;
            }
            dismiss();
            if (listener != null) {
                listener.clickConfirm(selBirdAdapter.mBirdBeans.get(position).getRawId());
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mAudioPlayer != null) {
            mAudioPlayer.pause();
        }

        if (mCurAudioPlayer != null) {
            mCurAudioPlayer.pause();
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConfirm(Integer id);
    }
}
